<template>
  <div class="search-panel">
    <el-row class="m-header-searchbar">
      <el-col :span="3" class="left"
        ><img
          src="//s0.meituan.net/bs/fe-web-meituan/e5eeaef/img/logo.png"
          alt="美团"
      /></el-col>
      <el-col :span="15" class="center">
        <div class="wrapper">
          <el-input
            v-model="searchVal"
            @focus="focus"
            @blur="blur"
            @input="input"
            placeholder="搜索商家和地点"
          />
          <button class="el-button el-button--primary">
            <i class="el-icon-search" />
          </button>
          <dl class="hotPlace" v-show="isHotPlace">
            <dt>热门搜索</dt>
            <dd v-for="(item, idx) in $store.state.home.hotPlace.slice(0, 5)" :key="idx">{{ item.name }}</dd>
          </dl>
          <dl class="searchList" v-show="isSearchList">
            <dd v-for="(item, idx) in searchArrList" :key="idx">
              {{ item.name }}
            </dd>
          </dl>
        </div>
        <p class="suggset">
          <a href="javasript:;" v-for="(item, idx) in $store.state.home.hotPlace.slice(6)" :key="idx">{{ item.name }}</a>
        </p>
        <ul class="nav">
          <li>
            <nuxt-link to="javascript:;" class="takeout">美团外卖</nuxt-link>
          </li>
          <li>
            <nuxt-link to="javascript:;" class="movie">猫眼电影</nuxt-link>
          </li>
          <li>
            <nuxt-link to="javascript:;" class="hotel">美团酒店</nuxt-link>
          </li>
          <li>
            <nuxt-link to="javascript:;" class="apartment">民宿/公寓</nuxt-link>
          </li>
          <li>
            <nuxt-link to="javascript:;" class="business">商家入驻</nuxt-link>
          </li>
        </ul>
      </el-col>
      <el-col :span="6" class="right">
        <ul class="security">
          <li>
            <i class="refund" />
            <p class="txt">随时退</p>
          </li>
          <li>
            <i class="single" />
            <p class="txt">不满意免单</p>
          </li>
          <li>
            <i class="overdue" />
            <p class="txt">过期退</p>
          </li>
        </ul>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import _ from "lodash";
export default {
  data() {
    return {
      isSearch: false,
      searchVal: "", // 搜索框val
      hotPlaceList: [],
      searchArrList: []
    };
  },
  computed: {
    isHotPlace: function() {
      return this.isSearch && !this.searchVal;
    },
    isSearchList: function() {
      return this.isSearch && this.searchVal;
    }
  },
  methods: {
    focus: function() {
      this.isSearch = true;
    },
    blur: function() {
      let that = this;
      setTimeout(function() {
        that.isSearch = false;
      }, 300);
    },
    input: _.debounce(async function() {
      let that = this;
      let city = that.$store.state.geo.position.city.replace("市", "");
      let res = await that.$axios.get("/search/top", {
        params: {
          city,
          input: that.searchVal
        }
      });
      if (res.data.success) {
        that.searchArrList = res.data.top;
      }
    }, 300)
  }
};
</script>

<style lang="scss">
.suggset {
  padding-top: 8px;
  font-size: 12px;
  width: 550px;
  overflow: hidden;
  height: 25px;
  box-sizing: border-box;
  text-align: left;
  padding-left: 12px;
  a {
    color: #999;
    margin-right: 10px;
    margin-bottom: 3px;
    display: inline-block;
  }
}
</style>
